package day16.JDK5News;

import java.util.ArrayList;
import java.util.Iterator;

/*
 * 需求：ArrayList存储自定义对象并遍历。要求加入泛型，并用增强for遍历。
 *
 * A:迭代器
 * B:普通for
 * C：增强for
 */
public class StudentTestDemo {
    public static void main(String[] args) {
        //创建集合对象
        ArrayList<Student> arr = new ArrayList<Student>();

        //创建学生对象
        Student s1 = new Student("林青霞", 27);
        Student s2 = new Student("貂蝉", 22);
        Student s3 = new Student("杨玉环", 24);
        Student s4 = new Student("秋香", 21);
        Student s5 = new Student("王昭君", 23);

        //添加元素
        arr.add(s1);
        arr.add(s2);
        arr.add(s3);
        arr.add(s4);
        arr.add(s5);

        //迭代器对象遍历
        Iterator<Student> it = arr.iterator();
        while (it.hasNext()) {
            Student s = it.next();
            System.out.println(s.getName() + "---" + s.getName());
        }

        System.out.println("-------------");

        //用普通for
        for(int x=0;x<arr.size();x++){
            Student s = arr.get(x);
            System.out.println(s.getName()+"---"+s.getAge());
        }

        System.out.println("-------------");

        //用增强for
        for(Student s:arr){
            System.out.println(s.getName()+"---"+s.getAge());
        }




    }
}
